package com.byx.chatclient.adapter;

import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.byx.chatclient.R;
import com.byx.chatclient.bean.PersonInfoItem;
import com.byx.chatclient.entity.User;
import com.byx.chatclient.utils.TimeFormatUtil;
import com.byx.imclientutil.entity.BaseMessage;
import com.chad.library.adapter.base.BaseDelegateMultiAdapter;
import com.chad.library.adapter.base.delegate.BaseMultiTypeDelegate;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.facebook.drawee.view.SimpleDraweeView;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.List;

/**
 * PersonInfoAdapter
 *
 * @author Superb
 * @date 2021/4/26 23:08
 */
public class PersonInfoAdapter extends BaseDelegateMultiAdapter<PersonInfoItem,BaseViewHolder> {
/*    public static final int AVATAR = 0;
    public static final int ACCOUNT = 1;
    public static final int NICK_NAME = 2;
    public static final int AGE = 3;
    public static final int SEX = 4;
    public static final int DESCRIPTION = 5;
    public static final int TELEPHONE = 6;
    public static final int REAL_NAME = 7;
    public static final int EMAIL = 8;
    public static final int VOCATION = 9;*/

    public static final int IMAGE_TYPE = 1;
    public static final int TEXT_TYPE = 2;

    private static final int IMAGE_ITEM = R.layout.layout_person_image_item;
    private static final int TEXT_ITEM = R.layout.layout_person_text_item;

    public PersonInfoAdapter() {
        super();
        init();
    }

    private void init() {
        BaseMultiTypeDelegate<PersonInfoItem> multiTypeDelegate = new BaseMultiTypeDelegate<PersonInfoItem>() {
            @Override
            public int getItemType(@NotNull List<? extends PersonInfoItem> list, int i) {
                if (list.get(i) instanceof PersonInfoItem.TextPersonInfoItem){
                    return TEXT_TYPE;
                }else if (list.get(i) instanceof PersonInfoItem.ImagePersonInfoItem){
                    return IMAGE_TYPE;
                }
                return 0;
            }
        };
        multiTypeDelegate.addItemType(TEXT_TYPE,TEXT_ITEM)
                .addItemType(IMAGE_TYPE,IMAGE_ITEM);
        setMultiTypeDelegate(multiTypeDelegate);
        addChildClickViewIds(R.id.sdv_avatar);
    }

    @Override
    protected void convert(@NotNull BaseViewHolder baseViewHolder, PersonInfoItem personInfoItem) {
        baseViewHolder.setText(R.id.tv_item_name,personInfoItem.getItemName());
        baseViewHolder.setImageResource(R.id.iv_modify,personInfoItem.isCanModify() ? R.mipmap.ic_more_gray_64_xxxhdpi : 0);
        switch (baseViewHolder.getItemViewType()){
            case TEXT_TYPE:
                baseViewHolder.setText(R.id.tv_item_content, ((PersonInfoItem.TextPersonInfoItem) personInfoItem).getItemContent());
                break;
            case IMAGE_TYPE:
                ((SimpleDraweeView) baseViewHolder.getView(R.id.sdv_avatar)).setImageURI("file://" + ((PersonInfoItem.ImagePersonInfoItem) personInfoItem).getAvatar());
                break;
            default:break;
        }
    }

    public void updateInfo(int position, String text){
        PersonInfoItem.TextPersonInfoItem item = (PersonInfoItem.TextPersonInfoItem) getItem(position);
        item.setItemContent(text);
        setData(position, item);
    }

    public void updateImage(int position, String path){
        PersonInfoItem.ImagePersonInfoItem item = (PersonInfoItem.ImagePersonInfoItem) getItem(position);
        item.setAvatar(path);
        setData(position,item);
    }
}
